package com.common.app.dialog

import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.fragment.app.FragmentManager
import com.common.app.R
import com.common.base.app.fragment.BaseDialogFragment
import com.common.app.databinding.BaseCommonCloseDialogBinding
import com.common.base.app.extras.dp2px
import com.common.base.app.extras.isNoEmpty
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.yes
import com.common.base.utils.AppUtils
import com.common.base.utils.ViewUtils

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/9/1
 * <p>
 * 描述：底部带关闭按钮的对话框
 * <p>
 * 修订历史：
 */
class CommonCloseDialog : BaseDialogFragment<BaseCommonCloseDialogBinding>() {
    companion object {
        fun newInstance(): CommonCloseDialog {
            return CommonCloseDialog()
        }
    }

    //内容文本
    private var contentTxt = ""

    //内容文本颜色
    private var contentColor = R.color.color_333333

    //确认按钮的颜色
    private var confirmTxtColor = R.color.white

    //内容区域是否居中
    private var contentCenter: Boolean = true

    //内容区域填充View布局
    private var contentLayout: View? = null

    private var confirmBtTxt = ""

    //按钮点击弹框是否默认消失
    private var defDismiss: Boolean = true


    //是否显示中间按钮
    private var showConfirm = true

    //是否显示关闭按钮
    private var showClose = true

    //背景是否透明
    private var backgroundIsTransparent = false

    private var dialogHeight = 0

    private var onCommonDialogListener: OnCommonCloseDialogListener? = null

    //    private var scale: Float? = 0f
    var scale: Float = 0f

    override fun getViewBinding() = BaseCommonCloseDialogBinding.inflate(layoutInflater)

//    override fun getSelfHeight(): Int = ViewGroup.LayoutParams.WRAP_CONTENT

    override fun getSelfHeight(): Int {
        return (dialogHeight > 0).yes { dialogHeight }
            .otherwise { ViewGroup.LayoutParams.WRAP_CONTENT }
    }

    override fun getSelfWidth(): Int {
        ViewUtils.isFullScreen().yes {
            (scale > 0).yes {
                return (293 * scale + 0.5f).toInt()
            }

        }.otherwise {
            scale = context?.resources?.displayMetrics?.density!!
            return dp2px(293)
        }
        return dp2px(293)
    }

//    override fun onResume() {
//        super.onResume()
//        dialog?.window?.setLayout( dp2px(320), ViewGroup.LayoutParams.WRAP_CONTENT)
//    }

    override fun initView(view: View) {
        this.isCancelable = false
        setShowPositionCenter()
        mViewBinding.dialogContentLayout.removeAllViews()
        //确认按钮
        mViewBinding.dialogButtonConfirm.text = confirmBtTxt
        mViewBinding.dialogButtonConfirm.setTextColor(AppUtils.getColor(confirmTxtColor))
        ViewUtils.setVisible(mViewBinding.dialogButtonConfirm, showConfirm)

        //关闭按钮
        ViewUtils.setVisible(mViewBinding.dialogButtonClose, showClose)

        //中间内容
        //居中容器参数
        val layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.gravity = Gravity.CENTER

        backgroundIsTransparent.yes {
            mViewBinding.dialogRootLayout.getDelegate().setBackgroundColor(Color.TRANSPARENT)
        }
        if (contentTxt.isNoEmpty()) {
            val textView = TextView(context)
            textView.setTextColor(AppUtils.getColor(contentColor))
            textView.textSize = 16f
            textView.text = contentTxt
            textView.gravity = Gravity.CENTER
            val dp13 = dp2px(13)
            val dp43 = dp2px(43)
            textView.setPadding(dp43, dp13, dp43, dp13)
            //内容文本居中
            contentCenter.yes {
                mViewBinding.dialogContentLayout.addView(textView, layoutParams)
            }.otherwise { mViewBinding.dialogContentLayout.addView(textView) }
        } else if (contentLayout != null) {
            contentCenter.yes {
                mViewBinding.dialogContentLayout.addView(contentLayout, layoutParams)
            }.otherwise { mViewBinding.dialogContentLayout.addView(contentLayout) }
        }
    }

    override fun initEvent() {

        mViewBinding.dialogButtonConfirm.setOnClickListener {
            onCommonDialogListener?.onClick(DialogButtonPosition.MIDDLE)
            defDismiss.yes { dismiss() }

        }
        mViewBinding.dialogButtonClose.setOnClickListener {
            onCommonDialogListener?.onClick(DialogButtonPosition.BOTTOM)
            defDismiss.yes { dismiss() }

        }
    }


    override fun getTheme(): Int = R.style.dialog

    fun setContent(
        text: String,
        @ColorRes color: Int = R.color.color_333333,
        isCenter: Boolean = true
    ): CommonCloseDialog {
        contentTxt = text
        contentColor = color
        contentCenter = isCenter
        return this
    }

    //设置内容区域填充控件
    fun setContent(view: View, isCenter: Boolean = true): CommonCloseDialog {
        contentLayout = view
        contentCenter = isCenter
        return this
    }

    fun setConfirm(
        text: String,
        @ColorRes color: Int = R.color.white,
    ): CommonCloseDialog {
        confirmBtTxt = text
        confirmTxtColor = color
        return this
    }

    fun setOnCommonDialogListener(
        onCommonDialogListener: OnCommonCloseDialogListener,
        defDismiss: Boolean = true
    ): CommonCloseDialog {
        this.defDismiss = defDismiss
        this.onCommonDialogListener = onCommonDialogListener
        return this
    }

    //设置是否显示确定按钮
    fun setShowConfirm(showConfirm: Boolean): CommonCloseDialog {
        this.showConfirm = showConfirm
        return this
    }

    //设置是都显示关闭按钮
    fun setShowClose(showClose: Boolean): CommonCloseDialog {
        this.showClose = showClose
        return this
    }

    fun setBackgroundIsTransparent(backgroundIsTransparent: Boolean): CommonCloseDialog {
        this.backgroundIsTransparent = backgroundIsTransparent
        return this
    }

    fun setDialogHeight(dialogHeight: Int): CommonCloseDialog {
        this.dialogHeight = dialogHeight
        return this
    }

    interface OnCommonCloseDialogListener {
        fun onClick(buttonPosition: DialogButtonPosition)
    }

    enum class DialogButtonPosition {
        MIDDLE, BOTTOM
    }

    override fun show(manager: FragmentManager, tag: String?) {
        super.show(manager, "commonCloseDialog")
    }
}